<style lang="less">
	.segmented-control {
		display: flex;
		flex-direction: row;
		justify-content: center;
		width: 100%;
		font-size: 26upx;
		box-sizing: border-box;
		margin: 0 auto;
		overflow: hidden;
		color:rgba(51,51,51,1);
		background: #FFFFFF;
	}

	.segmented-control.text {
		border: 0;
		border-radius: 0
	}

	.segmented-control-item {
		flex: 1;
		text-align: center;
		line-height: 84rpx;
		box-sizing: border-box;
	}

	.segmented-control-item.active {
		color: #fff;
		background: url("https://xpandago.oss-cn-shanghai.aliyuncs.com/applet/tab_cp.png") no-repeat bottom center;
		background-size: 200upx;
	}

	.segmented-control-item.text {
		border-left: 0;
		color:rgba(51,51,51,1);
		font-size: 24rpx;
	}

	.segmented-control-item.text.active {
		border-bottom-style: solid;
		font-size: 24rpx;
	}

	.segmented-control-item:first-child {
		border-left-width: 0
	}
</style>
<template>
	<view :class="{ text: styleType === 'text' }" :style="{ borderColor: styleType === 'text' ? '' : activeColor }" class="segmented-control">
		<view v-for="(item, index) in values" :class="[{ text: styleType === 'text' }, { active: index === currentIndex }]" :key="index"  class="segmented-control-item" @click="_onClick(index)">
			{{item.name}}
		</view>
	</view>
</template>

<script>
	export default {
		name: 'UniSegmentedControl',
		props: {
			current: {
				type: Number,
				default: 0
			},
			values: {
				type: Array,
				default () {
					return []
				}
			},
			activeColor: {
				type: String,
				default: '#007aff'
			},
			styleType: {
				type: String,
				default: 'button'
			}
		},
		data() {
			return {
				currentIndex: 0
			}
		},
		watch: {
			current(val) {
				if (val !== this.currentIndex) {
					this.currentIndex = val
				}
			}
		},
		created() {
			this.currentIndex = this.current
		},
		methods: {
			_onClick(index) {
				if (this.currentIndex !== index) {
					this.currentIndex = index
					this.$emit('clickItem', index)
				}
			}
		}
	}
</script>
